package module

import (
	"gopkg/app/interfaces"
)

type ModuleLifeManager struct {
	awakeList      []interfaces.Awake
	initList       []interfaces.Init
	runList        []interfaces.Run
	beforeShutList []interfaces.BeforeShut
	shutList       []interfaces.Shut
}

func (ml *ModuleLifeManager) Awake() {
	for _, v := range ml.awakeList {
		v.Awake()
	}
}

func (ml *ModuleLifeManager) Init() {
	for _, v := range ml.initList {
		v.Init()
	}
}

func (ml *ModuleLifeManager) Run() {
	for _, v := range ml.runList {
		v.Run()
	}
}

func (ml *ModuleLifeManager) BeforeShut() {
	for _, v := range ml.beforeShutList {
		v.BeforeShut()
	}
}

func (ml *ModuleLifeManager) Shut() {
	for _, v := range ml.shutList {
		v.Shut()
	}
}

func (ml *ModuleLifeManager) RegisterModule(s interfaces.Module) {
	awake, ok := s.(interfaces.Awake)
	if ok {
		ml.awakeList = append(ml.awakeList, awake)
	}

	init, ok := s.(interfaces.Init)
	if ok {
		ml.initList = append(ml.initList, init)
	}

	run, ok := s.(interfaces.Run)
	if ok {
		ml.runList = append(ml.runList, run)
	}

	beforeShut, ok := s.(interfaces.BeforeShut)
	if ok {
		ml.beforeShutList = append(ml.beforeShutList, beforeShut)
	}

	shut, ok := s.(interfaces.Shut)
	if ok {
		ml.shutList = append(ml.shutList, shut)
	}
}
